import QtQuick 2.6
import QtQuick.Layouts 1.2
import QtQuick.Dialogs 1.0

import "../../../Base/UiBase"
import "../../../Base/Config"
import Utils 1.0

Rectangle {
    id: root
    color: Color.background

    MouseArea {
        z: -1
        anchors.fill: parent
    }
    ImagePicker {
        id: imagePicker
        onAccepted: {
            addImage(fileUrl)
        }
    }

    ColumnLayout {
        anchors.fill: parent
        spacing: 0

        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 65 * Size.ratio
            color: Color.foreground

            Text {
                anchors.centerIn: parent
                text: qsTr("发动态")
                font.pixelSize: Size.largeFont
            }
        }

        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: Color.background

            Item {
                anchors {
                    fill: parent
                    margins: 15 * Size.ratio
                }
                Text {
                    visible: input.length === 0
                    text: qsTr("来说点什么吧")
                    color: Color.virtualInput
                    font.pixelSize: Size.normalFont
                }

                TextEdit {
                    id: input
                    onTextChanged: if (length > Numeral.maxPostTextLength)
                                       text = text.substring(
                                                   0, Numeral.maxPostTextLength)

                    anchors.fill: parent
                    color: Color.input
                    font.pixelSize: Size.normalFont

                    wrapMode: TextInput.WrapAnywhere
                    selectByMouse: true
                }
            }
        }

        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.preferredHeight: 195 * Size.ratio
            color: Color.foreground

            Row {
                id: imagesRow
                anchors.fill: parent
                anchors.leftMargin: 17 * Size.ratio
                anchors.rightMargin: 18 * Size.ratio
                spacing: 15 * Size.ratio

                Canvas {
                    id: canvas
                    width: 160 * Size.ratio
                    height: 160 * Size.ratio
                    anchors.verticalCenter: parent.verticalCenter
                    onPaint: {
                        var a = 90 * Size.ratio
                        var m = canvas.width / 2
                        var xs = m - a / 2
                        var ctx = canvas.getContext('2d')
                        ctx.lineWidth = 1
                        ctx.strokeRect(0, 0, width, height)
                        ctx.beginPath()
                        ctx.moveTo(xs, m)
                        ctx.lineTo(xs + a, m)
                        ctx.moveTo(m, xs)
                        ctx.lineTo(m, xs + a)
                        ctx.stroke()
                    }
                    MouseArea {
                        anchors.fill: parent
                        onClicked: imagePicker.open()
                    }
                }
            }
        }

        Item {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.minimumHeight: Size.elementInterval
            Layout.maximumHeight: Size.elementInterval
        }
        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.preferredHeight: 80 * Size.ratio
            color: Color.foreground

            Text {
                x: 40 * Size.ratio
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("对谁可见")
                font.pixelSize: Size.largeFont
            }

            Text {
                anchors.verticalCenter: parent.verticalCenter
                anchors {
                    right: idicator.left
                    rightMargin: 15 * Size.ratio
                }

                text: qsTr("所有人")
                font.pixelSize: Size.largeFont
            }
            SvgzImage {
                id: idicator
                source: "../../../../resources/newPost/zhishi.svg"
                width: 16 * Size.ratio
                height: 29 * Size.ratio
                anchors {
                    verticalCenter: parent.verticalCenter
                    right: parent.right
                    rightMargin: 40 * Size.ratio
                }
            }
        }

        Item {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.minimumHeight: Size.elementInterval
            Layout.maximumHeight: Size.elementInterval
        }
        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.preferredHeight: 80 * Size.ratio
            color: Color.foreground

            Text {
                x: 40 * Size.ratio
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("我的位置")
                font.pixelSize: Size.largeFont
            }
        }
        Item {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.minimumHeight: Size.elementInterval
            Layout.maximumHeight: Size.elementInterval
        }
        Rectangle {
            Layout.alignment: Qt.AlignBottom
            Layout.fillWidth: true
            Layout.preferredHeight: 100 * Size.ratio
            color: Color.foreground

            RowLayout {
                anchors.fill: parent
                Item {
                    Layout.fillHeight: true
                    Layout.preferredWidth: parent.width / 2
                    Text {
                        x: 40 * Size.ratio
                        anchors.centerIn: parent
                        text: qsTr("取消")
                        font.pixelSize: Size.largeFont
                    }
                    MouseArea {
                        anchors.fill: parent
                        onClicked: root.visible = false
                    }
                }
                Item {
                    Layout.fillHeight: true
                    Layout.preferredWidth: parent.width / 2
                    Text {
                        x: 40 * Size.ratio
                        anchors.centerIn: parent
                        text: qsTr("发布")
                        font.pixelSize: Size.largeFont
                    }
                    MouseArea {
                        anchors.fill: parent
                        onClicked: root.visible = false
                    }
                }
            }
        }
    }
}
